/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package updateliveserver;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 *
 * @author A7med Kotb
 */
//class that calls script that updates live servers
public class UpdateLiveServer {

    private String type;
    private String ip;
    private String port;
    private String userName;
    private String password;
    Element settingelement;
    Connection c;

    public UpdateLiveServer(String LSObject) {
        StringTokenizer st = new StringTokenizer(LSObject);
        int i = 0;
        String[] data = new String[5];
        while (st.hasMoreTokens()) {
            data[i] = st.nextToken("\\-");
            i++;
        }
        type = data[0];
        ip = data[1];
        port = data[2];
        userName = data[3];
        password = data[4];
        try {
            LogFile.log(" System ", " now Parsing XML File and Try to get required Jar and Script Files Path required for Live Server :[" + ip + "]");
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbuilder = dfactory.newDocumentBuilder();
            Document xmldocument = null;
            if (OSDetector.isWindows()) {
                xmldocument = dbuilder.parse("C:\\Users\\A7med Kotb\\Documents\\NetBeansProjects\\trunk\\Book Dimension Project\\src\\java\\Configuration\\winconfig.xml");
            }
            if (OSDetector.isUnix()) {
                xmldocument = dbuilder.parse("C:\\Users\\A7med Kotb\\Documents\\NetBeansProjects\\trunk\\Book Dimension Project\\src\\java\\Configuration\\linconfig.xml");
            }
            settingelement = xmldocument.getDocumentElement();
        } catch (ParserConfigurationException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get  Pathes Required from XML File for the Live Server [" + ip + "],and the Exception is [" + ex + "]");
        } catch (SAXException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get  Pathes Required from XML File for the Live Server [" + ip + "],and the Exception is [" + ex + "]");
        } catch (IOException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get  Pathes Required from XML File for the Live Server [" + ip + "],and the Exception is [" + ex + "]");
        } catch (DOMException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get  Pathes Required from XML File for the Live Server [" + ip + "],and the Exception is [" + ex + "]");
        } 
        if (type.equalsIgnoreCase("book")) {
            loadBookData(c, ip, port, userName, password);
        }
        if (type.equalsIgnoreCase("media")) {
            loadMediaData(c, ip, port, userName, password);
        }
    }

    //method to update book live server
    public void loadBookData(Connection c, String ip, String port, String userName, String password) {
        try {
            LogFile.log(" System ", " found that the Type of the Live Server with the IP :[" + ip + "], that will run Task now is [Book]");
            LogFile.log(" System ", " Now will try to Connect to the Live Server :["+ip+"]");
            String schemaName="test";
            String URL="jdbc:mysql://"+ip+":"+port+"/"+schemaName;
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            c = DriverManager.getConnection(URL, userName, password);
            LogFile.log(" System ", " Connected to the Live Server :["+ip+"] ,Successffully");
            LogFile.log(" System ", " Now will try to get all required data for Book Live Servers from The XML File");
            Element updateBookLiveServerelement = (Element) settingelement.getElementsByTagName("update-book-server").item(0);
            Element bookFilePathelement = (Element) settingelement.getElementsByTagName("book-file-path").item(0);
            String bookServerPath = updateBookLiveServerelement.getTextContent();
            String bookFilePath = bookFilePathelement.getTextContent();
            LogFile.log(" System ", " finished retreiving all required data for Book Live Servers from The XML File");
            ScriptRunner sr = new ScriptRunner(c, true, false);
            sr.runScript(new FileReader(bookServerPath), bookFilePath);
        } catch (DOMException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Book Live Server Connection, the Exception is [" + ex + "]");
        } catch (FileNotFoundException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Book Live Server Connection, the Exception is [" + ex + "]");
        }catch(SQLException ex){
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Book Live Server Connection, the Exception is [" + ex + "]");
        }
    }

    //method to update media live servers
    public void loadMediaData(Connection c, String ip, String port, String userName, String password) {
        try {
            LogFile.log(" System ", " found that the Type of the Live Server with the IP :[" + ip + "], that will run Task now is [Media]");
            LogFile.log(" System ", " Now will try to Connect to the Live Server :["+ip+"]");
            String schemaName="test";
            String URL="jdbc:mysql://"+ip+":"+port+"/"+schemaName;
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            c = DriverManager.getConnection(URL, userName, password);
            LogFile.log(" System ", " Connected to the Live Server :["+ip+"] ,Successffully");
            LogFile.log(" System ", " Now will try to get all required data for Media Live Servers from The XML File");
            Element updateMediaLiveServerelement = (Element) settingelement.getElementsByTagName("update-media-server").item(0);
            Element mediaFilePathelement = (Element) settingelement.getElementsByTagName("media-file-path").item(0);
            String mediaServerPath = updateMediaLiveServerelement.getTextContent();
            String mediaFilePath = mediaFilePathelement.getTextContent();
            LogFile.log(" System ", " Finished retreiving all required data for Media Live Servers from The XML File");
            ScriptRunner sr = new ScriptRunner(c, true, false);
            sr.runScript(new FileReader(mediaServerPath), mediaFilePath);
        } catch (DOMException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Media Live Server Connection, the Exception is [" + ex + "]");
        } catch (FileNotFoundException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Media Live Server Connection, the Exception is [" + ex + "]");
        }catch(SQLException ex){
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Media Live Server Connection, the Exception is [" + ex + "]");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        UpdateLiveServer uls = new UpdateLiveServer(args[0]);
    }
}
